package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnyqService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.web.form.YwYnJzfqwczzPfsnForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IPfYqService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 排放 延期
 * 
 * @author Administrator
 * 
 */

@Service
@Transactional
public class PfYqServiceImpl implements IPfYqService{
	Logger logger = (Logger) LoggerFactory.getLogger(PfYqServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwYnJzfqwczzPfsnyqService ywYnJzfqwczzPfsnyqService;
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String pfYqfilepath = localResource.getString("pfYqfilepath");

	// 附件存放路径
	private final String pfYqfujianpath = localResource.getString("pfYqfujianpath");

	// 解析成功反馈xml存放路径
	private final String pfYqfeedbackPath = localResource.getString("pfYqfeedbackPath");

	// 解析异常xml存放路径
	private final String pfYqexceptionPath = localResource.getString("pfYqexceptionPath");

	// 解析后，xml备份
	private final String pfYqbackPath = localResource.getString("pfYqbackPath");

	private Map<String, String> pfYqKeyMap;// xml内容的key

	private Map<String, String> pfYqKeyMapHead;// xml头部的key

	private Map<String, Long> pfYqfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initpfYqMap() {
		// 初始化数据部分对应的map
		pfYqKeyMap = new HashMap<String, String>();
		pfYqKeyMap.put("approvalNo", "approvalNo");
		pfYqKeyMap.put("businessCode", "businessCode");
		pfYqKeyMap.put("YSL_NO", "YSL_NO");
		pfYqKeyMap.put("sendTime", "sendTime");
		
		pfYqKeyMap.put("GCDZ", "GCDZ");
		pfYqKeyMap.put("SQDW", "SQDW");
		pfYqKeyMap.put("GCMC", "GCMC");
		pfYqKeyMap.put("JSDW_DZ", "JSDW_DZ");
		pfYqKeyMap.put("JSDW_LXR", "JSDW_LXR");
		pfYqKeyMap.put("JSDW_LXR_LXDH", "JSDW_LXR_LXDH");
		pfYqKeyMap.put("SGDW_GZ", "SGDW_GZ");
		pfYqKeyMap.put("SGDW_XMJL", "SGDW_XMJL");
		pfYqKeyMap.put("SGDW_XMJL_LXDH", "SGDW_XMJL_LXDH");
		pfYqKeyMap.put("SGDW_LXR", "SGDW_LXR");
		pfYqKeyMap.put("SGDW_LXR_LXDH", "SGDW_LXR_LXDH");
		pfYqKeyMap.put("YSGS", "YSGS");
		pfYqKeyMap.put("YSGS_LXR", "YSGS_LXR");
		pfYqKeyMap.put("YSGS_LXR_LXDH", "YSGS_LXR_LXDH");
		pfYqKeyMap.put("YSGS_KGRQ", "YSGS_KGRQ");
		pfYqKeyMap.put("YSGS_WGRQ", "YSGS_WGRQ");
		pfYqKeyMap.put("SBJZFQWCZL", "SBJZFQWCZL");
		pfYqKeyMap.put("CWZTFL", "CWZTFL");
		pfYqKeyMap.put("JCZTFL", "JCZTFL");
		pfYqKeyMap.put("ZHTFL", "ZHTFL");
		pfYqKeyMap.put("DXSTFL", "DXSTFL");
		pfYqKeyMap.put("QTTFL", "QTTFL");
		

		// 初始化头部对应的map
		pfYqKeyMapHead = new HashMap<String, String>();
		pfYqKeyMapHead.put("sn", "sn");
		pfYqKeyMapHead.put("sender", "sender");
		// pfYqKeyMapHead.put("receiver", "receiver");
		pfYqKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		pfYqfujianMap = new HashMap<String, Long>();
		pfYqfujianMap.put("FJ_01", 874l);
		pfYqfujianMap.put("FJ_02", 875l);
		pfYqfujianMap.put("FJ_03", 876l);
		pfYqfujianMap.put("FJ_04", 877l);
		pfYqfujianMap.put("FJ_05", 878l);
		pfYqfujianMap.put("FJ_06", 879l);
		pfYqfujianMap.put("FJ_07", 880l);
		pfYqfujianMap.put("FJ_08", 881l);
		pfYqfujianMap.put("FJ_09", 882l);
		pfYqfujianMap.put("FJ_10", 883l);
		pfYqfujianMap.put("FJ_11", 884l);
		pfYqfujianMap.put("FJ_12", 885l);
		pfYqfujianMap.put("FJ_13", 886l);
		pfYqfujianMap.put("FJ_14", 887l);
		pfYqfujianMap.put("FJ_15", 889l);
		pfYqfujianMap.put("FJ_16", 890l);
		pfYqfujianMap.put("FJ_17", 891l);
		pfYqfujianMap.put("FJ_18", 892l);
		pfYqfujianMap.put("FJ_19", 893l);
		pfYqfujianMap.put("FJ_20", 894l);
		pfYqfujianMap.put("FJ_21", 895l);
		pfYqfujianMap.put("FJ_22", 896l);
		pfYqfujianMap.put("FJ_23", 897l);
		pfYqfujianMap.put("FJ_24", 898l);
		pfYqfujianMap.put("FJ_25", 899l);
		pfYqfujianMap.put("FJ_26", 900l);
		pfYqfujianMap.put("FJ_27", 901l);
		pfYqfujianMap.put("FJ_28", 902l);
		pfYqfujianMap.put("FJ_29", 903l);
		pfYqfujianMap.put("FJ_30", 904l);
		pfYqfujianMap.put("FJ_31", 905l);
		pfYqfujianMap.put("FJ_32", 906l);
		pfYqfujianMap.put("FJ_33", 907l);
		pfYqfujianMap.put("FJ_34", 908l);
		pfYqfujianMap.put("FJ_35", 909l);
		pfYqfujianMap.put("FJ_36", 910l);
		pfYqfujianMap.put("FJ_37", 911l);
		pfYqfujianMap.put("FJ_38", 912l);
		pfYqfujianMap.put("FJ_39", 913l);
		pfYqfujianMap.put("FJ_40", 914l);
		pfYqfujianMap.put("FJ_41", 915l);
		pfYqfujianMap.put("FJ_42", 916l);
		pfYqfujianMap.put("FJ_43", 917l);
		pfYqfujianMap.put("FJ_44", 918l);
		pfYqfujianMap.put("FJ_45", 919l);
		pfYqfujianMap.put("FJ_46", 920l);
		pfYqfujianMap.put("FJ_47", 921l);
		pfYqfujianMap.put("FJ_48", 922l);
		pfYqfujianMap.put("FJ_49", 923l);
		pfYqfujianMap.put("FJ_50", 924l);
		pfYqfujianMap.put("FJ_51", 925l);
		pfYqfujianMap.put("FJ_52", 926l);
		pfYqfujianMap.put("FJ_53", 927l);
		pfYqfujianMap.put("FJ_54", 928l);
		pfYqfujianMap.put("FJ_55", 929l);
		pfYqfujianMap.put("FJ_56", 930l);
		pfYqfujianMap.put("FJ_57", 931l);
		pfYqfujianMap.put("FJ_58", 932l);
		pfYqfujianMap.put("FJ_59", 933l);
		pfYqfujianMap.put("FJ_60", 934l);
		pfYqfujianMap.put("FJ_61", 935l);
		pfYqfujianMap.put("FJ_62", 936l);
		pfYqfujianMap.put("FJ_63", 937l);
		pfYqfujianMap.put("FJ_64", 938l);
		pfYqfujianMap.put("FJ_65", 939l);
		pfYqfujianMap.put("FJ_66", 940l);
		pfYqfujianMap.put("FJ_67", 941l);
		pfYqfujianMap.put("FJ_68", 942l);
		pfYqfujianMap.put("FJ_69", 943l);
		pfYqfujianMap.put("FJ_70", 944l);
		pfYqfujianMap.put("FJ_71", 945l);
		pfYqfujianMap.put("FJ_72", 946l);
	}

	public void readXml(String projectPath) {
		if (pfYqKeyMap == null)
			this.initpfYqMap();
		File file = new File(pfYqfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(pfYqKeyMap, filePath,pfYqfeedbackPath, pfYqexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(pfYqKeyMapHead,files[i], pfYqfeedbackPath, pfYqexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczzPfsnForm form = new YwYnJzfqwczzPfsnForm();
						for (String mapKey : pfYqKeyMap.keySet()) {
							
							form.setYslNo(parseXMLService.getMapValue(pfYqKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(pfYqKeyMap.get("SQDW")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(pfYqKeyMap.get("SQDW")),"jzfqwczzpf_yq");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setAddress(parseXMLService.getMapValue(pfYqKeyMap.get("ADDRESS")));
							form.setGcdz(parseXMLService.getMapValue(pfYqKeyMap.get("GCDZ")));
							form.setGcmc(parseXMLService.getMapValue(pfYqKeyMap.get("GCMC")));
							form.setJsdwDz(parseXMLService.getMapValue(pfYqKeyMap.get("JSDW_DZ")));
							form.setJsdwLxr(parseXMLService.getMapValue(pfYqKeyMap.get("JSDW_LXR")));
							form.setJsdwLxrLxdh(parseXMLService.getMapValue(pfYqKeyMap.get("JSDW_LXR_LXDH")));
							form.setSgdwGz(parseXMLService.getMapValue(pfYqKeyMap.get("SGDW_GZ")));
							form.setSgdwXmjl(parseXMLService.getMapValue(pfYqKeyMap.get("SGDW_XMJL")));
							form.setSgdwXmjlLxdh(parseXMLService.getMapValue(pfYqKeyMap.get("SGDW_XMJL_LXDH")));
							form.setSgdwLxr(parseXMLService.getMapValue(pfYqKeyMap.get("SGDW_LXR")));
							form.setSgdwLxrLxdh(parseXMLService.getMapValue(pfYqKeyMap.get("SGDW_LXR_LXDH")));
							form.setYsgs(parseXMLService.getMapValue(pfYqKeyMap.get("YSGS")));
							form.setYsgsLxr(parseXMLService.getMapValue(pfYqKeyMap.get("YSGS_LXR")));
							form.setYsgsLxrLxdh(parseXMLService.getMapValue(pfYqKeyMap.get("YSGS_LXR_LXDH")));
							form.setYsgsKgrq(parseXMLService.getMapValue(pfYqKeyMap.get("YSGS_KGRQ")));
							form.setYsgsWgrq(parseXMLService.getMapValue(pfYqKeyMap.get("YSGS_WGRQ")));
							form.setSbjzfqwczl(parseXMLService.getMapValue(pfYqKeyMap.get("SBJZFQWCZL")));
							form.setCwztfl(parseXMLService.getMapValue(pfYqKeyMap.get("CWZTFL")));
							form.setJcztfl(parseXMLService.getMapValue(pfYqKeyMap.get("JCZTFL")));
							form.setZhtfl(parseXMLService.getMapValue(pfYqKeyMap.get("ZHTFL")));
							form.setDxstfl(parseXMLService.getMapValue(pfYqKeyMap.get("DXSTFL")));
							form.setQttfl(parseXMLService.getMapValue(pfYqKeyMap.get("QTTFL")));

							form.setBranch("jzfqwczzpf_yq");
							form.setApprovalNo(parseXMLService.getMapValue(pfYqKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(pfYqKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(pfYqKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywYnJzfqwczzPfsnyqService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									pfYqKeyMapHead, pfYqfeedbackPath, pfYqexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, pfYqfujianpath, pfYqfeedbackPath, pfYqexceptionPath, pfYqKeyMapHead,
								files, pfYqfujianMap, i, form.getQyid(), form.getId(), "jzfqwczzpf_yq");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								pfYqKeyMapHead, pfYqfeedbackPath, pfYqexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, pfYqbackPath + fileName,fileName);
				}
			}
		}
	}
}
